package edu.imac.angryfood.utils;

import org.jbox2d.dynamics.ContactListener;
import org.jbox2d.dynamics.contacts.ContactPoint;
import org.jbox2d.dynamics.contacts.ContactResult;

/**
 * Class that checks contacts between the objects in the scene
 *  
 */
public class ContactListenerModified implements ContactListener {

	/**
	 * Add the shapes in contact when there is a contact between two objects
	 * @param point it contains the shape 1 and shape 2 of the contact
	 */
	@Override
	public void add(ContactPoint point) {
		
		org.jbox2d.dynamics.World world = point.shape1.getBody().getWorld();

		if(world.compteurContact == 2) {
			world.compteurContact = 0;
		}
		
		if(world.compteurContact % 2 == 0) {
			world.addShape(point.shape1);
			world.addShape(point.shape2);
		}
		
		world.compteurContact++;
	}

	/**
	 * When the contact persists, we remove the shapes
	 * @param point it contains the shape 1 and shape 2 of the contact
	 */
	@Override
	public void persist(ContactPoint point) {
		org.jbox2d.dynamics.World world = point.shape1.getBody().getWorld();
		world.removeShape(point.shape1);
		world.removeShape(point.shape2);
	}

	/**
	 * When the contact is over, we remove the shapes
	 * @param point it contains the shape 1 and shape 2 of the contact
	 */
	@Override
	public void remove(ContactPoint point) {
		org.jbox2d.dynamics.World world = point.shape1.getBody().getWorld();
		world.removeShape(point.shape1);
		world.removeShape(point.shape2);
	}

	@Override
	public void result(ContactResult point) {
		
	}

}
